package com.gec.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gec.common.R;
import com.gec.entity.Category;
import com.gec.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  分类管理
 * </p>
 *
 * @author : [chenxys]
 * @version : [v1.0]
 * @createTime : [2022/7/5 16:37]
 */
@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 查询所有分类
     * Cacheable 缓存 key:键 value:值
     * @author chenxys
     * @return com.gec.common.R<com.gec.entity.Category>
     */
    @GetMapping("/list")
    public R<List<Category>> list(@RequestParam(required = false) Long type){
        log.info("查询所有的分类");
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(!ObjectUtils.isEmpty(type),Category::getType,type);
        queryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
        List<Category> list = categoryService.list(queryWrapper);
        return R.success(list);
    }


    /**
     * 分页查询
     * @author chenxys
     * @param page 当前页
     * @param pageSize 每页显示条数
     * @return com.gec.common.R<com.baomidou.mybatisplus.extension.plugins.pagination.Page>
     */
    @GetMapping("/page")
    public R<Page> page(Long page, Long pageSize){
        Page<Category> pageInfo = new Page<>(page, pageSize);
        // 构造排序条件
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(Category::getSort);
        // 分页查询
        categoryService.page(pageInfo,queryWrapper);
        log.info("分类管理,分页查询");
        return R.success(pageInfo);
    }

    /**
     * 修改分类
     * @author chenxys
     * @param category 分类信息
     * @return com.gec.common.R<java.lang.String>
     */
    @PutMapping
    public R<String> update(@RequestBody Category category){
        log.info("修改分类");
        boolean update = categoryService.updateById(category);
        if (update){
            return R.success("修改成功");
        }
        return R.error("修改失败");
    }

    /**
     * 保存新分类
     * @author chenxys
     * @param category 分类信息
     * @return com.gec.common.R<java.lang.String>
     */
    @PostMapping
    public R<String> save(@RequestBody Category category){
        log.info("保存新分类");
        boolean update = categoryService.save(category);
        if (update){
            return R.success("新建成功");
        }
        return R.error("新建失败");
    }

    /**
     * 删除分类
     * @author chenxys
     * @param ids 分类ID
     * @return com.gec.common.R<java.lang.String>
     */
    @DeleteMapping
    public R<String> delete(Long ids){
        log.info("删除分类");
        categoryService.remove(ids);
        return R.success("删除成功");
    }

}
